#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

// 1、无头+单向+非循环链表增删查改实现 

typedef int SLTDateType;
typedef struct SListNode {
	SLTDateType data;
	struct SListNode* next;
}SLNode;


// 动态申请一个节点 
extern SLNode* BuySListNode(SLTDateType x);

// 单链表打印 
void SListPrint(SLNode* plist);

// 单链表尾插 
void SListPushBack(SLNode** pplist, SLTDateType x);


// 动态申请一个节点 
SLNode* BuySListNode(SLTDateType x)
{
	SLNode* node = (SLNode*)malloc(sizeof(SLNode));
	if (NULL == node){
		assert(0);
		return NULL;
	}
	node->data = x;
	node->next = NULL;
	return node;
}

// 单链表打印 
void SListPrint(SLNode* plist)
{

	SLNode* cur = plist;
	while (cur){                //如果链表不为空
		printf("%d->", cur->data);
		cur = cur->next;
	}

	printf("NULL\n");
}

// 单链表尾插 
void SListPushBack(SLNode** pplist, SLTDateType x)
{
	assert(pplist);         //检验链表是否存在
	if (NULL == *pplist){        //如果链表为空
		*pplist = BuySListNode(x);
		return;
	}
	else{                    //链表不为空
		SLNode* cur = *pplist;
		while (cur->next){
			cur = cur->next;
		}
		cur->next = BuySListNode(x);
	}
}


struct ListNode* removeElements(struct SListNode* head, int val){
	if (head == NULL){
		return NULL;
	}
	struct SListNode* del = head;
	struct SListNode* cur = del;
	while (del != NULL){
		if (del->data == val){
			if (head == del){             //要删除head节点
				head = head->next;
				free(del);
				del = head;
			}
			else{
				cur->next = del->next;
				free(del);
				del = cur->next;
			}
		}
		else{
			cur = del;
			del = cur->next;
		}
	}
	return head;
}

int main()
{
	SLNode* sl = NULL;
	SListPushBack(&sl, 7);
	SListPushBack(&sl, 7);
	SListPushBack(&sl, 7);
	SListPushBack(&sl, 7);
	SListPrint(sl);
	removeElements(sl, 7);
	SListPrint(sl);
	return 0;
}